package com.example.zqh.controller;

import com.example.zqh.model.RespBean;
import com.example.zqh.utils.MinioUtilS;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@Slf4j
@Api(tags = "minio文件上传测试类")
public class MinioController {
    @Autowired
    private MinioUtilS minIoUtil;
    @Autowired
    private MinioUtilS minioUtilS;
    @Value("${minio.endpoint}")
    private String address;
    @Value("${minio.bucketName}")
    private String bucketName;



    /**
      * @Author: zqh
      * @Date:
      * @Description: 文件上传接口，上传的文件会生成一个随机的名字
    */
    @PostMapping("/upload")
    @ApiOperation(value = "文件上传")
    public RespBean upload(MultipartFile file) {



        List<String> upload = minioUtilS.upload(new MultipartFile[]{file});
        String fileFormat = upload.get(0).substring(upload.get(0).length()-4);
        if (!fileFormat.equals(".jpg")&&!fileFormat.equals(".png")&&!fileFormat.equals(".gif")&&!fileFormat.equals(".mp4")&&!fileFormat.equals(".mp3")){
            minioUtilS.delete(bucketName,upload.get(0));
            return RespBean.error("文件上传失败，只支持jpg,png,gif,mp4格式");

        }
        else {
        return RespBean.success("文件上传成功","http://43.138.31.196:9000"+"/"+bucketName+"/"+upload.get(0));
        }
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 文件下载接口，通过文件名可以下载该文件
    */
//    @GetMapping("/download")
//    @ApiOperation(value = "通过文件名下载文件")
//
//    public ResponseEntity<byte[]> download(){
//        ResponseEntity<byte[]> download = minioUtilS.download("C400004LZwE13k5hzj_1648190083798.m4a");
//        return download;
//
//    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 通过文件的名字，把他在minio里面删除掉
    */
//    @DeleteMapping("/delete")
//    @ApiOperation(value = "通过桶名和文件名删除minio里面的文件")
//
//    public void delete(String fileName){
//        minioUtilS.delete(bucketName,fileName);
//    }

}



